package com.example.lp.lpuicore.anim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.example.lp.lpuicore.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Author by lp,on 2021/7/1/001,14:22.
 * 属性动画
 */
public class PropertyAnimFragment extends BaseFragment {
    private AnimatorSet animatorSet;
    private List<Animator> valueAnimatorList;

    @Override
    int getFragmentLayoutId() {
        return R.layout.fragment_property_amin;
    }

    @Override
    String getTitle() {
        return "属性动画";
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initData();
        initView();
        return view;
    }

    private void initData() {
        /**
         * playTogether和playSequentially只负责激活控件动画，后续的动画还是由ObjectAnimator自己控制。
         * playSequentially是一个动画执行完后执行下一个动画，但如果前一个动画是无限循环，下一个动画永远不会执行。
         * */
        valueAnimatorList = new ArrayList<>();
        valueAnimatorList.add(ObjectAnimator.ofFloat(baseAnimView, "rotationX", 0, 360));
        valueAnimatorList.add(ObjectAnimator.ofFloat(baseAnimView, "rotationY", 0, 360));
        valueAnimatorList.add(ObjectAnimator.ofInt(baseAnimView, "backgroundColor", Color.RED, Color.BLUE));
        valueAnimatorList.add(ObjectAnimator.ofFloat(baseAnimView, "scaleX", 1, 0.5f));
        valueAnimatorList.add(ObjectAnimator.ofFloat(baseAnimView, "scaleY", 1, 0.5f));
        valueAnimatorList.add(ObjectAnimator.ofFloat(baseAnimView, "alpha", 1, 0.5f, 1));
    }

    private void initView() {
        getFragmentView().findViewById(R.id.btn_trans).setOnClickListener(v -> {
            ObjectAnimator.ofFloat(baseAnimView, "translationY", baseAnimView.getHeight()).start();
        });
        getFragmentView().findViewById(R.id.btn_object_alpha).setOnClickListener(v -> {
            valueAnimatorList.get(5).start();
        });
        getFragmentView().findViewById(R.id.btn_color).setOnClickListener(v -> {
            valueAnimatorList.get(2).start();
        });
        getFragmentView().findViewById(R.id.btn_object_rotate).setOnClickListener(v -> {
            animatorSet = new AnimatorSet();
            animatorSet.play(valueAnimatorList.get(0)).after(valueAnimatorList.get(1));
            animatorSet.start();
        });
        getFragmentView().findViewById(R.id.btn_object_scale).setOnClickListener(v -> {
            animatorSet = new AnimatorSet();
            animatorSet.play(valueAnimatorList.get(3)).after(valueAnimatorList.get(4));
            animatorSet.start();
        });
        getFragmentView().findViewById(R.id.btn_AnimatorSet).setOnClickListener(v -> {
            animatorSet = new AnimatorSet();
            animatorSet.setDuration(5 * 1000);
            animatorSet.playTogether(valueAnimatorList);
            animatorSet.start();
        });
        getFragmentView().findViewById(R.id.btn_Sequentially).setOnClickListener(v -> {
            animatorSet = new AnimatorSet();
            animatorSet.setDuration(1 * 1000);
            animatorSet.playSequentially(valueAnimatorList);
            animatorSet.start();
        });
        /**
         *
         * public Builder with(Animator anim) 和前面动画一起执行
         * public Builder before(Animator anim) 执行前面的动画后再执行该动画
         * public Builder after(Animator anim) 先执行这个动画再执行前面动画
         * public Builder after(long delay) 延迟n毫秒之后执行动画
         */
        getFragmentView().findViewById(R.id.btn_after).setOnClickListener(v -> {
            animatorSet = new AnimatorSet();
            animatorSet.setDuration(1 * 1000);
            animatorSet.play(valueAnimatorList.get(0)).after(1000).with(valueAnimatorList.get(1)).after(valueAnimatorList.get(2));
            animatorSet.start();
        });
        getFragmentView().findViewById(R.id.btn_valueAnim).setOnClickListener(v -> {
            ValueAnimator animator = ValueAnimator.ofFloat(baseAnimView.getX(), 600f);
            animator.setDuration(2 * 1000);
            animator.addUpdateListener(animation -> {
                float animatedValue = (float) animation.getAnimatedValue();
                Log.i("PropertyAnimFragment", "animatedValue: " + animatedValue);
                baseAnimView.setX(animatedValue);
            });

            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    Log.e("PropertyAnimFragment", "onAnimationEnd: ");
                    baseAnimView.setX(0);
                }
            });
            animator.start();
        });


    }
}
